package cn.com.xyc.rabbitmq;

import java.io.IOException;
import java.util.Date;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class EmitLog {
	private final static String EXCHANGE_NAME = "ex_log";

	public static void main(String[] args) throws IOException {
		// 创建连接和频道
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("192.168.1.128");
		factory.setPort(5672);
		factory.setUsername("xyc");
		factory.setPassword("123456");
		Connection connection = factory.newConnection();
		Channel channel1 = connection.createChannel();
		// 声明转发器和类型
		channel1.exchangeDeclare(EXCHANGE_NAME, "fanout");

		String message = new Date().toLocaleString() + "：log something";
		// 往转发器发送消息
		channel1.basicPublish(EXCHANGE_NAME, "", null, message.getBytes());

		System.out.println("[x] Sent '" + message + "'");
		channel1.close();
		connection.close();
	}
}
